package cn.wolfcode.luowowo.website.controller;

import cn.wolfcode.luowowo.article.domain.Destination;
import cn.wolfcode.luowowo.article.domain.StrategyContent;
import cn.wolfcode.luowowo.article.domain.StrategyDetail;
import cn.wolfcode.luowowo.article.service.IDestinationService;
import cn.wolfcode.luowowo.article.service.IStrategyCommendService;
import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.article.service.IStrategyTagService;
import cn.wolfcode.luowowo.cache.key.RedisKey;
import cn.wolfcode.luowowo.cache.service.IStrategyStatsCacheService;
import cn.wolfcode.luowowo.cache.vo.StrategyStats;
import cn.wolfcode.luowowo.comment.domain.StrategyComment;
import cn.wolfcode.luowowo.comment.query.StrategyCommentQueryObject;
import cn.wolfcode.luowowo.comment.service.IStrategyCommentService;
import cn.wolfcode.luowowo.common.query.StrategyQueryObject;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.search.query.SearchQueryObject;
import cn.wolfcode.luowowo.search.service.IStrategySearchService;
import cn.wolfcode.luowowo.website.annotation.LoginUser;
import cn.wolfcode.luowowo.website.annotation.RequiredLogin;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("strategy")
public class StrategyController {

    @Reference
    private IDestinationService destinationService;
    @Reference
    private IStrategyTagService tagService;
    @Reference
    private IStrategyDetailService detailService;
    @Reference
    private IStrategyCommentService commentService;
    @Reference
    private IStrategyStatsCacheService strategyStatsCacheService;
    @Reference
    private IStrategyCommendService strategyCommendService;
    @Reference
    private IStrategySearchService strategySearchService;


    @RequestMapping("")
    public String index(Model model) {
        //攻略飙升排行 upCds
        model.addAttribute("upCds", strategyStatsCacheService.listRankTop10(RedisKey.STRATEGY_STATS_UP_SORT));
        //攻略热门排行 hotCds
        model.addAttribute("hotCds", strategyStatsCacheService.listRankTop10(RedisKey.STRATEGY_STATS_HOT_SORT));

        //commends  推荐5篇攻略
        model.addAttribute("commends", strategyCommendService.listTop5());

        // 国内省份攻略 unabroads
        model.addAttribute("unabroads",strategySearchService.listCondition(SearchQueryObject.CONDITION_UNABROAD));
        //海外攻略 abroads
        model.addAttribute("abroads",strategySearchService.listCondition(SearchQueryObject.CONDITION_ABROAD));
        //主题攻略themes
        model.addAttribute("themes",strategySearchService.listCondition(SearchQueryObject.CONDITION_THEME));

        //themeCds 主题推荐
        model.addAttribute("themeCds",strategySearchService.themeCommend());

        return "strategy/index";
    }


    @RequestMapping("/list")
    public String list(@ModelAttribute("qo") StrategyQueryObject qo, Model model) {
//     吐司
        List<Destination> toasts = destinationService.getToasts(qo.getDestId());
        model.addAttribute("toasts", toasts);

        model.addAttribute("dest", toasts.remove(toasts.size() - 1));
        //tags 标签 通过目的地id,查询标签.
        model.addAttribute("tags", tagService.listByDestId(qo.getDestId()));

        //pageInfo 分页对象
        model.addAttribute("pageInfo", detailService.query(qo));

        return "strategy/list";
    }

    @RequestMapping("detail")
    public String detail(Long id, @LoginUser UserInfo user, Model model) {
        StrategyDetail detail = detailService.get(id);
        StrategyContent content = detailService.getContent(id);
        detail.setStrategyContent(content);
        model.addAttribute("detail", detail);


        //阅读数+1
        strategyStatsCacheService.incr(id, 1, IStrategyStatsCacheService.VIEWNUM_TYPE);
        //vo  缓存对象
        model.addAttribute("vo", strategyStatsCacheService.getStats(id));

        boolean favor = user != null ? strategyStatsCacheService.isFavor(user.getId(), id) : false;
        //isFavor 回显当前的收藏结果
        model.addAttribute("isFavor", favor);

        //飙升排行榜+1
        strategyStatsCacheService.addRank(RedisKey.STRATEGY_STATS_UP_SORT, 1, id);


        return "strategy/detail";
    }

    //添加评论
    @RequiredLogin
    @RequestMapping("/commentAdd")
    @ResponseBody
    public AjaxResult commentAdd(StrategyComment entity, @LoginUser UserInfo user) {
        AjaxResult result = new AjaxResult();
        //填充当期那登录用户数据
        entity.setHeadUrl(user.getHeadImgUrl());
        entity.setUserId(user.getId());
        entity.setUsername(user.getNickname());
        //保存操作
        commentService.save(entity);

        Long detailId = entity.getDetailId();
        //评论数+1
        strategyStatsCacheService.incr(detailId, 1, IStrategyStatsCacheService.REPLYNUM_TYPE);
        StrategyStats stats = strategyStatsCacheService.getStats(detailId);

        //飙升排行榜 评论+1
        strategyStatsCacheService.addRank(RedisKey.STRATEGY_STATS_UP_SORT, 1, detailId);

        //返回评论数据
        return result.addData(stats.getReplynum());
    }

    //查询当前攻略(文章)下的评论
    @RequestMapping("/comment")
    public String comment(StrategyCommentQueryObject qo, Model model) {
        Page<StrategyComment> page = commentService.query(qo);
        model.addAttribute("page", page);
        return "strategy/commentTpl";
    }

    //评论点赞
    @RequiredLogin
    @RequestMapping("/commentThumbUp")
    @ResponseBody
    public AjaxResult strategyThumbup(String toId, Long fromId) {
        commentService.strategyThumbup(toId, fromId);
        return new AjaxResult();
    }

    //收藏
    @RequiredLogin
    @RequestMapping("/favor")
    @ResponseBody
    public AjaxResult favor(Long strategyId, @LoginUser UserInfo user) {
        AjaxResult result = new AjaxResult();
        //判断缓存对象是否含有当前用户的收藏
        boolean success = strategyStatsCacheService.favor(strategyId, user.getId());
        //返回是否成功的结果
        result.setSuccess(success);
        //返回收藏数
        StrategyStats stats = strategyStatsCacheService.getStats(strategyId);

        //热门排行榜
        int num = success ? 1 : -1;
        strategyStatsCacheService.addRank(RedisKey.STRATEGY_STATS_HOT_SORT, num, strategyId);


        return result.addData(stats.getFavornum());
    }

    //置顶点赞
    @RequiredLogin
    @RequestMapping("/strategyThumbup")
    @ResponseBody
    public AjaxResult strategyThumbup(Long strategyId, @LoginUser UserInfo user) {
        AjaxResult result = new AjaxResult();
        //判断是否置顶点赞过
        boolean success = strategyStatsCacheService.strategyThumbup(user.getId(), strategyId);
        result.setSuccess(success);

        //热门排行榜
        if (success) {
            strategyStatsCacheService.addRank(RedisKey.STRATEGY_STATS_HOT_SORT, 1, strategyId);
        }


        //返回点赞数
        StrategyStats stats = strategyStatsCacheService.getStats(strategyId);
        return result.addData(stats.getThumbsupnum());
    }


    @RequestMapping("searchPage")
    public String searchPage(SearchQueryObject qo,Model model){
        //page
        model.addAttribute("page",strategySearchService.query(qo));
        return"strategy/searchPageTpl";
    }


}
